﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;


namespace ServeNHL.Models
{
    [MetadataType(typeof(eventSignUpMetadata))]
    public partial class EVENT_SIGNUP
    {
        /// <summary>
        /// updates or inserts into DB
        /// </summary>
        /// <param name="events"></param>
        /// <returns>signupID</returns>
        public int SaveEventSU(eventViewModel events)
        {
            int signupID = 0;

            if (events.eventSignUp.SIGNUP_ID >= 0)
            {
                UpdateEventSignUp(events.eventSignUp);

            }
            else
            {
                InsertEventSignUp(events.eventSignUp);
            }

            signupID = events.eventSignUp.SIGNUP_ID;

            return signupID;
        }//end of public int SaveEventSU(eventViewModel events)


        /// <summary>
        /// inserts event signup into DB, called by SaveEventSU
        /// </summary>
        /// <param name="signUp"></param>
        /// <returns>signUpID</returns>
        public int InsertEventSignUp(EVENT_SIGNUP signUp) {

            int signUpID = 0;

            using (var dbconn = new fclick_dbEntities())
            {
                var curEvtSu = new EVENT_SIGNUP
                {
                    EVENT_ID = signUp.EVENT_ID,
                    USER_ID = signUp.USER_ID,
                    GUEST_FLAG = signUp.GUEST_FLAG,
                    NUM_GUESTS =signUp.NUM_GUESTS,
                    STATUS=signUp.STATUS,
                    CREATED_DATE = Helpers.createDate.getCreateDate(),
                    LAST_MOD_DATE = Helpers.createDate.getCreateDate(),

                };
                dbconn.EVENT_SIGNUP.Add(curEvtSu);
                dbconn.SaveChanges();

                signUpID = curEvtSu.SIGNUP_ID;


            }//end of using (var dbconn = new fclick_dbEntities())

            return signUpID;

        }//end of  public int InsertEventSignUp(eventViewModel signUp)

        /// <summary>
        /// Updates Event to DB, called by SaveEventSignUp
        /// </summary>
        /// <param name="evtSU"></param>
        /// <returns>signUpID</returns>
        public int UpdateEventSignUp(EVENT_SIGNUP evtSU)
        {
            int signUpID = 0;
            using (var dbconn = new fclick_dbEntities())
            {
                var tempEvtSU = new EVENT_SIGNUP();

                if (evtSU.EVENT_ID >= 0)
                {
                    tempEvtSU.EVENT_ID = evtSU.EVENT_ID;
                }

                if (evtSU.USER_ID >= 0)
                {
                    tempEvtSU.USER_ID = evtSU.USER_ID;
                }

                tempEvtSU.GUEST_FLAG = evtSU.GUEST_FLAG;
              
                if (evtSU.NUM_GUESTS >= 0)
                {
                    tempEvtSU.NUM_GUESTS = evtSU.NUM_GUESTS;
                }

                if (!String.IsNullOrEmpty(evtSU.STATUS))
                {
                    tempEvtSU.STATUS = evtSU.STATUS;
                }

                dbconn.EVENT_SIGNUP.Add(tempEvtSU);
                dbconn.SaveChanges();

                signUpID = tempEvtSU.SIGNUP_ID;
            }//end of using (var dbconn = new fclick_dbEntities)

            return signUpID;
        }//end of public int UpdateEventSignUp(EVENT_SIGNUP evtSU)

    }//end of public partial class eventSignUp
}